// @flow
import React from 'react'
import styles from './ModelView.css'
import { notification } from 'antd'
import Models from 'components/Models'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import { connect } from 'react-redux'
// const { Header, Footer, Sider, Content } = Layout
type Props = {
  history: Object,
  match: Object,
  deviceId: number
}

type States = {
  datas: Array<Object>,
  loading: boolean
}
class ModelBase extends React.Component {
  props: Props
  state: States
  constructor (props: Props) {
    super(props)
    this.state = {
      datas: [],
      loading: false
    }
  }
  addTask = (modelId: string) => {
    this.props.history.push({
      pathname: '/analysis/task-add',
      state: {
        modelId: modelId
      }
    })
  }

  componentWillMount () {
    this.setState({
      loading: true
    })
    const { deviceId } = this.props
    universalFetch(`${__SERVICE_API__}analysis/models?deviceId=${deviceId}`)
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          notification.error({
            message: '提醒',
            description: json.message
          })
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        this.setState({
          datas: json.result,
          loading: false
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }
  render () {
    const { datas, loading } = this.state
    const { match, history } = this.props
    return (
      <div className={styles['main']}>
        {
          datas.map((data, i) => {
            return <Models {...{
              key: i,
              data,
              loading,
              match,
              history
            }} />
          })
        }
      </div>
    )
  }
}

const mapStatesToProps = (state) => {
  return {
    deviceId: state.deviceCard.deviceData.id
    // deviceId: state.deviceCard.groupId
  }
}
export default connect(mapStatesToProps, {})(ModelBase)
